﻿#region Copyright information
// 
// Copyright © 2012-2013 Yongkee Cho. All rights reserved.
// 
// This code is a part of the SubnetworkToolkit and governed under the terms of the
// GNU Lesser General  Public License (LGPL) version 2.1 which accompanies this distribution.
// For more information on the LGPL, please visit http://bol.codeplex.com/license.
// 
// - Filename: Program.cs
// - Author: Yongkee Cho
// - Email: yongkeecho@outlook.com
// - Date Created: 2013-01-30 2:55 PM
// - Last Modified: 2013-01-30 3:21 PM
// 
#endregion
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using BOL.IO;
using CommandLine;
using SubnetworkToolkit.IO;

namespace SubnetworkToolkit.GreedySearch
{
    static class Program
    {
        static void Main(string[] args)
        {
            var options = new Options();
            
            try
            {
                // parse arguments
                var parser = new CommandLineParser(new CommandLineParserSettings(Console.Error));
                parser.ParseArguments(args, options);

                if (args.Length == 0 || args[0] == "-h")
                    Console.Error.Write(options.GetUsage());
                else
                {
                    var networkFile = options.NetworkFile;
                    var depth = options.Depth;
                    var degree = options.Degree;
                    var fraction = options.Fraction;
                    var statisticsFile = options.StatisticsFile;
                    var threshold = options.Threshold;
                    var outputFile = options.OutputFile;
                    var isVerbose = options.IsVerbose == 1;
                    var top = options.Top;
                    var bypass = options.Bypass.Select(Int32.Parse).ToList();

                    // reads expression file
                    var dtr = new DelimitedTextReader(statisticsFile, ",");
                    var statistics = dtr.Read().ToDictionary(line => Int32.Parse(line[0]), line => Double.Parse(line[1]));
                    dtr.Close();

                    var sr = new StreamReader(networkFile);
                    var dictionary = new Dictionary<int, List<int>>();
                    while (!sr.EndOfStream)
                    {
                        var line = sr.ReadLine();
                        if (line != null)
                        {
                            var tmp =
                                line.Split(new[] {'\t'}, StringSplitOptions.RemoveEmptyEntries).Select(Int32.Parse).
                                    ToList();
                            dictionary.Add(tmp[0], tmp.Skip(1).Select(x => x).ToList());
                        }
                    }
                    sr.Close();

                    // generate max possible number of nodes;
                    var gs = new GreedySearch(dictionary, statistics, threshold, bypass, depth, degree, fraction, isVerbose);
                    var subnets = gs.Search(top).Select((subnet, index) => new GeneSet("Subnet" + index, subnet));   

                    // create output directory
                    var gmt = new GmtWriter(outputFile, subnets);
                    gmt.Write();
                    gmt.Close();
                }
            }
            catch (Exception ex)
            {
                Console.Error.Write(ex.Message);
            }
        }
    }
}
